package cn.tsoft.taobao.logic;

import java.util.List;
import java.util.Map;


import cn.tsoft.taobao.db.TaoBaoDataManager;
import cn.tsoft.taobao.ui.ProductItem;
import cn.tsoft.taobao.ui.RecommendItemDetailInfoActivity;
import cn.tsoft.taobao.ui.TradeOrderActivity;
import cn.tsoft.taobao.util.LogInInfo;
import cn.tsoft.vo.ItemForDetail;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

public class MainService extends Service implements Runnable{


	//线程是否运行的标示
	public boolean isrun=true;
	
	//更新UI
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
		 ITaoBaoActivity itb ;
			switch(msg.what)
			{
			case Task.ONSALE_PRODUCT_ITEM://获取店铺的正在买的商品的信息
				itb=(ITaoBaoActivity)TaskManagerApplication.getActivityByName("ProductItem");
				itb.refresh(ProductItem.REFRESH_PRODUCT,msg.obj);
				 break;
			case Task.TASK_USER_LOGIN:
				 itb=(ITaoBaoActivity)TaskManagerApplication.getActivityByName("LoginActivity");
				 itb.refresh();
				 break;
			case Task.TRADE_ORDER_ITEM:
				 itb=(ITaoBaoActivity)TaskManagerApplication.getActivityByName("TradeOrderActivity");
				 itb.refresh(TradeOrderActivity.REFRESH_TRADEORDER,msg.obj);
				 break;
			case Task.TASK_GET_PRODUCT_INFO:
				 itb=(ITaoBaoActivity)TaskManagerApplication.getActivityByName("RecommendItemDetailInfoActivity");
				 itb.refresh(RecommendItemDetailInfoActivity.LOADE_PRODUCT,msg.obj);
				 break;
			}
		}
	};
	
	
	@Override
	public void run() {
		//线程一直在运行
			while(isrun){
				    Task lastTask = null;
					synchronized (TaskManagerApplication.allTask) {
						 if(TaskManagerApplication.allTask.size()>0){
							 //获取工作
							 lastTask = TaskManagerApplication.allTask.get(0);
							 doTask(lastTask);
						 }
					}//线程相应的休眠1秒
					try{Thread.sleep(1000);}catch(Exception ex){};
			}
	}
	//执行任务
	private void doTask(Task  task) {
		 Message message = handler.obtainMessage();
		 message.what =  task.getTaskID() ; //定义刷新UI的状态
		 List<Map<String, Object>> result ;
			try{
					switch (task.getTaskID()) {
					case Task.ONSALE_PRODUCT_ITEM://获取店铺的正在买的商品的信息
						result = TaoBaoDataManager.getOnSaleProductList();
						message.obj= result;
						break;
					case Task.TASK_USER_LOGIN://用户登录
						   LogInInfo.getAuthorInfo();//验证用户登录
						break;
					case Task.TRADE_ORDER_ITEM://交易订单列表
//						 result = TaoBaoDataManager.getTradeOrderList();
//						 message.obj= result;
						break;
					case Task.TASK_GET_PRODUCT_INFO://获取商品详细信息
						 Map<String, Object>  param = task.getTaskParam();
						 ItemForDetail item = TaoBaoDataManager.getProductInfo(param.get("num_id").toString());
						 message.obj= item;
						break;
					default:
						break;
					}
			}catch(Exception e){
				message.what = -1;
			}
		   handler.sendMessage(message);//发送更新UI的消息给主线程
		   TaskManagerApplication.removeTask(task);//执行完任务
	}
	

	@Override
	public void onCreate() {
		super.onCreate();
		isrun = true;
		Thread thread = new Thread(this);
		thread.start() ; 
	}
	

	@Override
	public void onDestroy() {
		super.onDestroy();
		isrun = false;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
}
